<?php

namespace App\Http\Controllers\Manage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Category;
use Validator;
use App\Article;

class CategoryController extends Controller
{
    /**
     * @var int 数据表id
     */
    public $table_id = 2;

    /**
     * CaseController constructor.
     */
    public function __construct()
    {
        parent::__construct($this->table_id);

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        #判断权限
        if($this->rule['view'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        $categorys = Category::all();
        return view('manage/category/index', compact('categorys'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        #判断权限
        if($this->rule['add'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        $category = Category::all();
        return view('manage/category/create', compact('category'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $callback = null;
        $input = $request->only('parent', 'name');
        $category = new Category;

        // 查找重复
        if(!$input['name']) {
            $callback = [
                'code' => 101,
                'msg' => '分类名称不能为空！'
            ];
            return json_encode($callback);
        }
        if(Category::whereName($input['name'])->first()) {
            $callback = [
                'code' => 101,
                'msg' => '重复添加！'
            ];
        }
        else {
            if(!$input['parent']) {
                if(Category::insert(['name' => $input['name'], 'parent' => 0])) {
                    $callback = [
                        'code' => 100,
                        'msg' => '添加成功！'
                    ];
                }
                else {
                    $callback = [
                        'code' => 101,
                        'msg' => '添加失败！'
                    ];
                }
            }
            else {
                if(Category::insert(['name' => $input['name'], 'parent' => $input['parent']])) {
                    $callback = [
                        'code' => 100,
                        'msg' => '添加成功！'
                    ];
                }
                else {
                    $callback = [
                        'code' => 101,
                        'msg' => '添加失败！'
                    ];
                }
            }
        }

        return json_encode($callback);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        #判断权限
        if($this->rule['update'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        $category_id = Category::where('id', $id)->get();
        $category = Category::all();
        return view('manage/category/edit', compact('category_id', 'category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $callback = null;
        $input = $request->only('name', 'parent');
        $rule = [
            'name' => 'required',
        ];
        $message = [
            'name.required' => '分类名称不能为空'
        ];
        $validator = Validator::make($input, $rule, $message);
        if($validator->fails()) {
            $warnings = $validator->messages();
            $ShowWarnings = $warnings->first();
            $callback = [
                'msg' => $ShowWarnings
            ];
        }
        else {
            // 查找重复
            if(Category::whereName($input['name'])->first()) {
                $callback = [
                    'code' => 103,
                    'msg' => '该分类已存在！'
                ];
            }
            else {
                if(!$input['parent']) {
                   if(Category::where('id', $id)->update(['name' => $input['name']])) {
                       $callback = [
                           'code' => 100,
                           'msg' => '分类更新成功！'
                       ];
                   }
                   else {
                       $callback = [
                           'code' => 102,
                           'msg' => '分类更新失败！'
                       ];
                   }
                }
                else {
                    if(Category::where('id', $id)->update(['name' => $input['name'], 'parent' => $input['parent']])) {
                        $callback = [
                            'code' => 100,
                            'msg' => '分类更新成功！'
                        ];
                    }
                    else {
                        $callback = [
                            'code' => 102,
                            'msg' => '分类更新失败！'
                        ];
                    }
                }
            }
        }
        return json_encode($callback);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $callback = null;
        #判断权限
        if($this->rule['delete'] == 0){
            return [
                'code' => 101,
                'msg' => '权限不足！'
            ];
        }
        if(Article::where('category', '=', $id)->first()) {
            $callback = [
                'code' => 101,
                'msg' => '请先删除分类中的文章！'
            ];
        }
        else {
            if(Category::destroy($id)) {
                $callback = [
                    'code' => 100,
                    'msg' => '删除成功！'
                ];
            }
            else {
                $callback = [
                    'code' => 101,
                    'msg' => '删除失败！'
                ];
            }
        }
        return $callback;
    }
}
